<template>
  <work-template @submitOn="submitOn" @closeSelectedTag="closeSelectedTag"
                 :flowId="flowId"
                 v-loading="loading"
                 :butShow="bool">
    <div slot="form">
      <mergePage v-model="ruleForm" :state="state" ref="refMergePage"/>
    </div>

  </work-template>
</template>

<script>
import * as apis from "@/api/returnMargin";

export default {
  name: 'ReturnMarginMerge',
  components: {
    mergePage: () => import('./components/mergePage.vue')
  },
  computed: {
    flowId() {
      return this.$route.query?.flowId
    },
    bool() {
      if (this.$route.query.type === "edit") {
        return true;
      }
      if (this.$route.query.type === "view") {
        return false;
      }
    },
    state(){
      if (this.$route.query.type === "edit") {
        return 'write';
      }
      if (this.$route.query.type === "view") {
        return 'readonly';
      }
    }
  },
  watch: {},
  data() {
    return {
      ruleForm: {
        returnCarDepositList: [],
        returnCarReductionList: []
      },
      loading: false
    };
  },
  created() {
    if (this.$route.query.type === 'edit' && this.$route.query.id) {
      if (typeof this.$route.query.id === 'string') {
        this.handleDetails([this.$route.query.id])
      } else {
        this.handleDetails(this.$route.query.id)
      }
    }
    if (this.$route.query.type === 'view' && this.$route.query.id) {
      this.handleDetail(this.$route.query.id)
    }
  },
  mounted() {

  },
  methods: {
    //合并 详情
    async handleDetails(id) {
      this.loading = true
      try {
        let res = await apis?.queryList(id)
        this.ruleForm = res.data

        if (['', null, undefined, []].includes(this.ruleForm.returnCarReductionList)) {
          console.log(this.ruleForm.returnCarReductionList,"--1---")
          this.ruleForm.returnCarReductionList = []
          this.ruleForm.idList = id
        }
        if(this.ruleForm.returnCarDepositList[0].returnCategory === '5'){
          this.ruleForm.deductionStatus = '0'
        }
        console.log(this.ruleForm.returnCarReductionList,"---2--")
      } catch (e) {
        console.error(e)
      } finally {
        this.loading = false
      }
    },

    //查看详情
    async handleDetail(id) {
      this.loading = true
      try {
        let res = await apis?.queryDetails(id)
        this.ruleForm = res?.data || {}
      } catch (e) {
        console.error(e)
      } finally {
        this.loading = false
      }
    },

    async submitOn() {
      let loading = null;
      try {
        await this.$refs.refMergePage.handleEffect();
        loading = this.$loading({
          lock: true,
          text: "保存中",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)",
        });
        let res = await apis?.update(this.ruleForm);
        if (res.code === 200) {
          this.closeSelectedTag();
        }
      } catch (e) {
        console.error(e);
      } finally {
        loading?.close();
      }
    },

    closeSelectedTag() {
      this.$tab.closePage().then(({visitedViews}) => {
        this.$router.push({
          path: "/ReturnCar/ReturnMarginList",
        });
      });
    }
  },
};
</script>

<style scoped lang=""></style>


